package com.xh.bussiness.tiku.controller;

import com.xh.bussiness.tiku.bean.request.QuestionImportLogRequest;
import com.xh.bussiness.tiku.model.QuestionImportLog;
import com.xh.bussiness.tiku.service.IQuestionImportLogService;
import com.xh.bussiness.tiku.service.IQuestionService;
import com.xh.core.bean.AjaxResult;
import com.xh.core.bean.XhExportHeaderBean;
import com.xh.core.bean.XhPage;
import com.xh.core.constant.AjaxResultConstant;
import com.xh.core.controller.BaseController;
import com.xh.core.util.StringUtils;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * <p>
 * 试题导入记录表 前端控制器
 * </p>
 *
 * @author JQM
 * @since 2019-07-05
 */
@Api(tags="题库-试题导入记录接口")
@RestController
@RequestMapping("/admin/questionImportLog")
public class QuestionImportLogController extends BaseController {

    @Autowired
    private IQuestionImportLogService questionImportLogService;
    @Autowired
    private IQuestionService questionService;
    /**
     * 分页数据查询
     * @param page 分页对象
     * @param bean 查询条件Bean
     * @return
     */
    //旧版需删除
    @ApiOperation(value="分页数据查询", notes = "分页数据查询")
    @PostMapping("/queryPage")
    public XhPage queryPage(HttpServletRequest request,XhPage page, QuestionImportLogRequest bean){
        if (StringUtils.isEmpty(bean.getUserId())) {
            String userId = request.getHeader("userId");
            bean.setUserId(userId);
        }
        page=questionImportLogService.queryQuestionImportLogPage(page,bean);
        return page;
    }

    @ApiOperation(value="分页数据查询", notes = "分页数据查询")
    @RequestMapping("/queryPageV2")
    public AjaxResult queryPageV2(HttpServletRequest request,XhPage page, QuestionImportLogRequest bean){
        if (StringUtils.isEmpty(bean.getUserId())) {
            String userId = request.getHeader("userId");
            bean.setUserId(userId);
        }
        page=questionImportLogService.queryQuestionImportLogPage(page,bean);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, page);
    }


    /**
     * 数据查询
     * @param bean 查询条件Bean
     * @return
     */
    @ApiOperation(value="根据条件查询所有数据", notes = "根据条件查询所有数据")
    @PostMapping("/query")
    public AjaxResult query(QuestionImportLogRequest bean){
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",questionImportLogService.queryQuestionImportLog(bean));
    }

    /**
     * 导出全部
     * @param bean
     * @return
     */
    @ApiOperation(value="根据条件导出数据", notes = "根据条件导出数据")
    @PostMapping(value="/exportAll")
    public AjaxResult exportAll(QuestionImportLogRequest bean, HttpServletRequest request) {
        try {
            LinkedHashMap<String,XhExportHeaderBean> exportHeaderBeans=new LinkedHashMap<>();
            //exportHeaderBeans.put("username",new XhExportHeaderBean("用户名"));
            //exportHeaderBeans.put("deleted",new XhExportHeaderBean("是否删除",ImmutableMap.of(0,"否",1,"是")));
            //exportHeaderBeans.put("createTime",new XhExportHeaderBean("创建时间","yyyy-MM-dd HH:mm:ss"));

            String downloadUrl=questionImportLogService.exportAll(request,exportHeaderBeans,bean,"queryQuestionImportLogPage");
            if(downloadUrl!=null){
                return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",downloadUrl);
            }else{
                return new AjaxResult(AjaxResultConstant.Result.ERROR,"导出失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(AjaxResultConstant.Result.ERROR);
        }
    }

    /**
     * 新增和保存
     * @param questionImportLog 实体对象
     * @return
     */
    @ApiOperation(value="新增和保存", notes = "新增和保存")
    @PostMapping(value="/saveOrUpdate")
    @Transactional
    public AjaxResult saveOrUpdate(@Validated QuestionImportLog questionImportLog){
        if(questionImportLog.getId()==null){
            questionImportLogService.insert(questionImportLog);
        }else{
            questionImportLogService.update(questionImportLog);
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 删除操作
     * @param id
     * @return
     */
    @ApiOperation(value="删除操作", notes = "删除操作")
    @ApiImplicitParams({
        @ApiImplicitParam(name="id", value="导入记录ID", required = true, dataType = "String")
    })
    @PostMapping("/deleteById/{id}")
    @Transactional
    public AjaxResult deleteBybyIdId(@PathVariable String id){
        QuestionImportLog  log = questionImportLogService.getById(id);
        questionImportLogService.delete(id);

        String questionIds = log.getQuestionIds();
        if (!StringUtils.isEmpty(questionIds)) {
            String[] quesIds = questionIds.split(",");
            for (String quesId : quesIds) {
                questionService.deleteQuestion(quesId);
            }
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 批量删除操作
     * @param ids
     * @return
     */
    @ApiOperation(value="批量删除操作", notes = "批量删除操作")
    @ApiImplicitParams({
        @ApiImplicitParam(name="ids", value="导入记录ID数组", required = true, dataType = "String[]")
    })
    @PostMapping("/batchDelete")
    @Transactional
    public AjaxResult batchDelete(String[] ids){
        questionImportLogService.batchDelete(ids);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    @ApiOperation("根据导入记录查询试题")
    @RequestMapping("/queryQuestionByImportId")
    public AjaxResult queryQuestionByImportId(String importId){
        if (StringUtils.isEmpty(importId)) {
            return new AjaxResult(AjaxResultConstant.Result.ERROR, "参数错误");
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, questionImportLogService.queryQuestionByImportId(importId));
    }

    @ApiOperation("导入保留原卷绑定导入记录")
    @RequestMapping("/bindQuestionImportLogPaper")
    public AjaxResult bindQuestionImportLogPaper(String importLogId, String paperId) {
        if (StringUtils.isEmpty(importLogId) || StringUtils.isEmpty(paperId)) {
            return new AjaxResult(AjaxResultConstant.Result.ERROR, "参数错误");
        }
        QuestionImportLog log = questionImportLogService.getById(importLogId);
        if (log != null) {
            log.setPaperId(paperId);
            questionImportLogService.update(log);
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    @ApiModelProperty("引用相似试题到导入记录")
    @RequestMapping("/quoteQuestionToImportLog")
    public AjaxResult quoteQuestionToImportLog(String importId, String quoteQuestionId, String importQuestionId) {
        if (StringUtils.isEmpty(importId) || StringUtils.isEmpty(quoteQuestionId) || StringUtils.isEmpty(importQuestionId)) {
            return new AjaxResult(AjaxResultConstant.Result.ERROR, "参数错误");
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, questionImportLogService.quoteQuestionToImportLog(importId, quoteQuestionId, importQuestionId));
    }

    /**
     * 查询学科网导入试卷的试题
     * @author 赵文博
     * @date 2021/6/2 14:32
     * @return com.xh.core.bean.AjaxResult
     **/
    @PostMapping("/queryXKWQuestion")
    public AjaxResult queryXKWQuestion(String paperId){
        if (StringUtils.isEmpty(paperId)) {
            return new AjaxResult(AjaxResultConstant.Result.ERROR, "参数错误");
        }
        Map<String, Object> map = questionImportLogService.queryQuestionByPaperId(paperId);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,map);
    }
}

